package com.deep.transaction.demo8.service.impl;


import com.deep.transaction.demo8.dao.IUserDao;
import com.deep.transaction.demo8.service.IBalanceService;
import com.deep.transaction.demo8.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 */
@Service
public class BalanceServiceImpl implements IBalanceService {
    @Autowired
    @Qualifier("demo8UserDao")
    IUserDao userDao;

    @Autowired
    ILogService logService;

    /**
     * 无事务开启模式
     *
     * @param outAcct
     * @param inAcct
     */
    @Override
    public void noTrans(String outAcct, String inAcct) {
        // 转出
        userDao.subBalance(outAcct);
        int i = 1 / 0;
        // 入账
        userDao.addBalance(inAcct);
    }

    /*
     * 主事务
     * logService，新事务
     * */
    @Transactional
    @Override
    public void requiresNew(String outAcct, String inAcct) {
        // 事务隔离，创建新事务
        logService.log();
        // 转出
        userDao.subBalance(outAcct);
        int i = 1 / 0;
    }

    /*
     * @Transactional 可以标记在类上面（当前类所有的方法都运用上了事务）
     * @Transactional 标记在方法则只是当前方法运用事务
     * 建议@Transactional写在业务逻 辑层上，因为只有业务逻辑层才会有嵌套调用的情况。
     * */
    @Transactional
    @Override
    public void trans(String outAcct, String inAcct) {
        // 转出
        userDao.subBalance(outAcct);
        int i = 1 / 0;
        // 入账
        userDao.addBalance(inAcct);
    }


}
